package ptumall.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ptumall.model.shop.Goods;
import ptumall.model.shop.GoodsDetail;
import ptumall.service.GoodsService;
import ptumall.utils.ApiResponse;
import ptumall.utils.PageResult;

import java.util.Collections;
import java.util.Map;

@RestController
@RequestMapping("/api/shop")
@RequiredArgsConstructor
public class ShopController {
    private final GoodsService goodsService;

    //查询商品详细信息
    @GetMapping("/goods/{goodsId}")
    public ApiResponse<GoodsDetail> getGoodsDetail(@PathVariable Long goodsId) {
        return ApiResponse.success(goodsService.getGoodsDetail(goodsId));
    }

    @GetMapping("/search")
    public ResponseEntity<Map<String, Object>> search(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        PageResult<Goods> result = goodsService.search(keyword, page, pageSize);
        return ResponseEntity.ok(Collections.singletonMap("data", result));
    }
}